import { CITEXT } from "sequelize/types"
import { ScriptListService } from "../../service/scriptList"
import { C, N} from "../../config/commonTypes"
import { log } from "console";
import { Restfull } from "../../config/resModel";
import { FsCon } from "../../utils/fsController"
import { LSFS } from "../../utils/LSFS";
import { ErrorScriptName } from "../../config/resCodeMsg";

class _ScriptListCON {
   async getList(ctx:C) {
        //获取所有剧本列表
        const scriptList = await ScriptListService.getScriptList()
        // CITEXT.body = 
        console.log("查询结果:",scriptList);
    //   console.log("ctx", );
      
        ctx.body = Restfull.success({scriptList,gameStatus:ctx.gameStatus})
    }

    //获取某个单独剧本
    async getScript(ctx:C) {
        console.log("ctx",ctx.query.script_id);
        
        let { script_id } = ctx.query
        const script = await ScriptListService.getScript((script_id as string))
        // CITEXT.body = 
        console.log("查询结果:",script);
        ctx.body = Restfull.success({script})
    }

    async createScript(ctx:C) {
        const { scriptName, introduction,manNum,womenNum } = ctx.request.body.scriptBaseInfo
        console.log(typeof ctx.request.body.scriptBaseInfo.womenNum);
        // FsCon.createDirs("盒子+道具")
       try {
           //在数据库中添加该剧本
          const addScriptDB = await ScriptListService.createBaseScript({ scriptName, introduction,manNum,womenNum })
           //创建剧本文件夹
        const mkDirRes = await LSFS.createDirItems(scriptName);
        console.log("创建结果:", mkDirRes);
        if(mkDirRes){
            ctx.body = Restfull.success("创建成功")
            return
        }
        ctx.body = Restfull.faild(ErrorScriptName)
       } catch(err) {
           console.log("最后错误:", err);
        ctx.body = Restfull.faild({msg:"报错了"})          
       }
    }
}


const ScriptListCON = new _ScriptListCON()

export {
    ScriptListCON
}